package com.utils;

import java.lang.reflect.Field;

/**
 * Created by Administrator on 2017/6/1.
 */
public class ObjectUtil {
    public static void copyProperties(Object target, Object source) throws IllegalAccessException {
        Field[] field_sources = source.getClass().getDeclaredFields();
        Field[] fields_targets = target.getClass().getDeclaredFields();
        for (Field field_source : field_sources) {
            field_source.setAccessible(true);
            String name_source = field_source.getName();
            Object value_source = field_source.get(source);
            for (Field field_target : fields_targets) {
                field_target.setAccessible(true);
                String name_target = field_target.getName();
                if (name_source.equals(name_target)) {
                    field_target.set(target,value_source);
                }
            }
        }
    }
}
